<?php

namespace app\shopapi\controller;

use app\admin\model\shop\Category;
use app\common\controller\Shopapi;
use app\shopapi\model\pay\Alipay;
use app\shopapi\model\pay\ShopInfeed;
use app\shopapi\model\pay\Wechat;
use app\shopapi\model\Shop;
use app\shopapi\model\ShopAdmin;
use app\shopapi\model\ShopUser;
use fast\Rsa;
use think\Log;

/**
 * 店铺认证相关接口
 */
class Auth extends Shopapi
{
    protected $noNeedLogin = ['getArea','memberMerchantInfo','memberInfo','notify','memberBindbankCard','bankcardnotify','editMember','terminal_notify','ceshiOrderComplete'];
    protected $noNeedRight = '*';


    //地区
    public function getArea()
    {
        $model = new ShopInfeed();
        $res = $model->getArea();
        $this->success('',$res);
    }

    //token
    public function getToken()
    {
        $model = new ShopInfeed();
        $res = $model->getToken();
        $this->success('',$res);
    }

    //上传文件
    public function upload()
    {
        $image_type = input('image_type','');
        $image_name = input('image_name','');
        $image_content = input('image_content','');
        $model = new \app\shopapi\model\lakala\ShopInfeed();
        $res = $model->upload($image_type,$image_name,$image_content);
        $this->success('',$res);
    }

    //进件
    public function apply()
    {
        $shop_id = input('shop_id',0);
        try {
            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->apply($shop_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //测试进件
    public function ceshiapply()
    {
        $shop_id = input('shop_id',0);
        try {
            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->ceshiapply($shop_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }
    //分账撤销
    public function splitRevoke()
    {
        try {
            $model = new \app\shopapi\model\lakala\Pay();

            $res = $model->splitRevoke();

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }
    //分账
    public function ceshiOrderComplete()
    {
        try {
            $model = new \app\shopapi\model\lakala\Pay();

            $res = $model->ceshiOrderComplete();

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //查询会员信息
    public function memberInfo()
    {
        $out_member_no = input('out_member_no','');
        try {
            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->memberInfo($out_member_no);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //查询会员商户
    public function memberMerchantInfo()
    {
        $shop_id = input('shop_id',0);
        try {
            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->memberMerchantInfo($shop_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //查询分账比例
    public function memberSplitScale()
    {
        $shop_id = input('shop_id',0);
        try {
            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->memberSplitScale($shop_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //会员绑卡
    public function memberBindbankCard()
    {
        //$shop_id = input('shop_id',0);
        try {
            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->memberBindbankCard($this->shopauth->shop_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //会员信息变更
    public function editMember()
    {
        //$shop_id = input('shop_id',0);
        try {
            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->editMember($this->shopauth->shop_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //银行卡列表
    public function cardList()
    {
        //$shop_id = input('shop_id',0);
        try {
            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->cardList($this->shopauth->shop_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //会员商户增终
    public function addTerminal()
    {
        $shop_id = input('shop_id',0);
        try {
            $shop_info = Shop::get($shop_id);

            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->addTerminal($shop_info->member_no,$shop_info->channel_merchant_no);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //渠道子商户查询
    public function searchMerchant()
    {
        $shop_id = input('shop_id',0);
        try {

            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->searchMerchant($shop_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //店铺入驻回调
    public function notify()
    {
        $event_type = input('event_type');
        $event = input('event');
        Log::error('入驻回调:'.$event);
        $order_model = new Shop();

        $res = $order_model->notify($event,$event_type);

        $this->success('',$res);
    }

    //绑卡回调
    public function bankcardnotify()
    {
        $event_type = input('event_type');
        $event = input('event');
        Log::error('绑卡回调:'.$event);
        $order_model = new Shop();

        $res = $order_model->bindCardNotify($event,$event_type);

        $this->success('',$res);
    }

    //申请收银台回调
    public function terminal_notify()
    {
        $event_type = input('event_type');
        $event = input('event');
        Log::error('绑卡回调:'.$event);
        $order_model = new Shop();

        $res = $order_model->terminalNotify($event,$event_type);

        $this->success('success','SUCCESS');
    }

    //商户绑定回调
    public function bind_notify()
    {
        $event_type = input('event_type');
        $event = input('event');
        Log::error('商户绑定回调:'.$event_type);
        Log::error('商户绑定回调:'.$event);
    }

    //商户绑定
    public function bind()
    {
        try {
            $shop_id = input('shop_id',0);

            $channel_merchant_no = input('channel_merchant_no','');

            $channel_terminal_no = input('channel_terminal_no','');

            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->bindShop($channel_merchant_no,$channel_terminal_no,$shop_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //解绑
    public function unbind()
    {
        try {
            $shop_id = input('shop_id',0);

            $channel_merchant_no = input('channel_merchant_no','');

            $channel_terminal_no = input('channel_terminal_no','');

            $member_no = input('member_no','');

            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->merchantUnbind($member_no,$channel_merchant_no,$channel_terminal_no);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }
}